import type { PageLoad } from "./$types";
import {
  CellType,
  Align,
  type Column,
  CellEditComponentType,
} from "@component/table";
import {
  EntryType,
  validDir,
  type DatasetEntry,
} from "@store/entity/dataset/entry";

const buildDatasetEntryCols = (url: URL, baseUrl: string): Column[] => {
  return [
    {
      key: "type",
      title: "类型",
      width: 40,
      options: {
        type: CellType.Icon,
        icon: (rowData: DatasetEntry): string => {
          return rowData.type == EntryType.File ? "doc" : "folder";
        },
      },
    },
    {
      key: "name",
      title: "名称",
      width: 200,
      headAlign: Align.Left,
      options: {
        type: CellType.Link,
        align: Align.Left,
        editType: CellEditComponentType.Input,
        editable: true,
        editOption: {
          required: true,
        },
        linkOption: {
          href: (o: any) => {
            return o["type"] == EntryType.Directory
              ? `${url.pathname}/${o["name"]}`
              : `${baseUrl}/file/${o["id"]}/annotation`;
          },
          // matcher: (o: any) => {
          //   return o["type"] == EntryType.Directory
          // }
        },
      },
    },
    {
      key: "repo_url",
      title: "路径",
      options: {
        editType: CellEditComponentType.Input,
        editable: true,
      },
    },
  ];
};

export const load = (async ({ parent, url, params }) => {
  let data = await parent();
  console.log("data=>", data, url, params);
  const baseUrl = `/dataset/${params.dataset_code}`;
  return {
    title: data.menu_name || "数据集文件",
    dataEntriesCols: buildDatasetEntryCols(url, baseUrl),
    datasetCode: params.dataset_code,
    parent: validDir(params.parent),
    baseUrl,
  };
}) satisfies PageLoad;
